package hibernate;

import java.io.Serializable;
import java.util.List;

import javax.el.ELContext;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import utils.CryptUtils;
import bean.EleveBean;
import bean.PhotoBean;

public class EleveRepository extends GenericRepository<EleveBean, Integer> implements Serializable {

	private static final long serialVersionUID = 1L;

	public EleveBean GetByEmail(String email){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		trans.begin();
		Criteria criteria = s.createCriteria(EleveBean.class).add(Restrictions.eq("email_eleve", email));
		EleveBean ele = (EleveBean) criteria.uniqueResult();
		trans.commit();
		return ele;
	}

	public PhotoBean GetImageById(String id){
		int id2 = Integer.parseInt(id);
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(EleveBean.class).add(Restrictions.eq("id_eleve", id2));
		EleveBean ele = (EleveBean) criteria.uniqueResult();
		System.out.println(ele.getNom_eleve());
		trans.commit();
		return ele.getPhotoProfilImage();
	}

	public EleveBean GetById(Integer Id){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(EleveBean.class).add(Restrictions.eq("id_eleve", Id));
		EleveBean ele = (EleveBean) criteria.uniqueResult();
		trans.commit();
		return ele;
	}

	public List<EleveBean> GetAllByName(String name){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(EleveBean.class).add(Restrictions.like("nom_eleve", name+"%"));
		@SuppressWarnings("unchecked")
		List<EleveBean> lst = (List<EleveBean>) criteria.list();
		trans.commit();
		return lst;
	}

	@SuppressWarnings("unchecked")
	public List<EleveBean> GetAll(){
		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(EleveBean.class);
		List<EleveBean> lst = (List<EleveBean>) criteria.list();
		trans.commit();
		return lst ;
	}

	public boolean AuthentificationConnexion(EleveBean eleve){

		Session s = HelperSessionFactory.getSession();
		Transaction trans = s.beginTransaction();
		Criteria criteria = s.createCriteria(EleveBean.class).add(Restrictions.eq("email_eleve", eleve.getEmail_eleve()));
		EleveBean ele = (EleveBean) criteria.uniqueResult();

		if(ele == null){
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Utilisateur inconnu", "Utilisateur inconnu");
			FacesContext.getCurrentInstance().addMessage("Utilisateur inconnu", message);
			trans.commit();
			return false;
		}
		else {
			if(CryptUtils.StringToBytes(eleve.getPwd_eleve()).equals(ele.getCrypted_pwd())){
				this.setEleveBean(ele);
				trans.commit();
				return true;
			}
			else {
				FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Mot de passe incorrect", "Mot de passe incorrect");
				FacesContext.getCurrentInstance().addMessage("Mot de passe incorrect", message);
				trans.commit();
				return false;
			}
		}
	}

	public void setEleveBean(EleveBean eleveBean){

		// Contexte en cours
		ELContext elContext = FacesContext.getCurrentInstance().getELContext(); 
		EleveBean currentEleveBean = (EleveBean)FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "ObjetEleve");
		currentEleveBean.setId_eleve(eleveBean.getId_eleve());
		currentEleveBean.setId_classe_eleve(eleveBean.getId_classe_eleve());
		currentEleveBean.setNom_eleve(eleveBean.getNom_eleve());
		currentEleveBean.setPrenom_eleve(eleveBean.getPrenom_eleve());
		currentEleveBean.setPwd_eleve(eleveBean.getPwd_eleve());
		currentEleveBean.setPwd_eleve(eleveBean.getConfirm_pwd_eleve());
		currentEleveBean.setDate_naissance_eleve(eleveBean.getDate_naissance_eleve());
		currentEleveBean.setEmail_eleve(eleveBean.getEmail_eleve());
		currentEleveBean.setEntreprise_eleve(eleveBean.getEntreprise_eleve());
		currentEleveBean.setVille_eleve(eleveBean.getVille_eleve());
		currentEleveBean.setPhotoProfil(eleveBean.getPhotoProfil());
		currentEleveBean.setPhotoProfilImage(eleveBean.getPhotoProfilImage());

	}

	@Override
	public void Save(EleveBean obj) {
		obj.setCrypted_pwd(CryptUtils.StringToBytes(obj.getPwd_eleve()));
		super.Save(obj);
		setEleveBean(obj);
	}

}



